import { getS210motorList, getV90motorList } from '@/services/sales/salesNeedInfo';
import {
  PageContainer,
  ProCardTabsProps,
  ProFormInstance,
  ProFormMoney,
} from '@ant-design/pro-components';
import {
  ModalForm,
  ProCard,
  ProFormDependency,
  ProFormDigit,
  ProFormGroup,
  ProFormSelect,
  ProFormText,
  ProForm,
} from '@ant-design/pro-components';
import { Card, InputNumber, Select, Space } from 'antd';
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
import type { FormDialogProps } from './data';
import type { FormLayout } from 'antd/es/form/Form';
import access from '@/access';
import { useAccess, useLocation } from 'umi';
import { SettingFilled } from '@ant-design/icons';
const { Option } = Select;

const LAYOUT_TYPE_HORIZONTAL = 'horizontal';
const G120modal: React.FC<FormDialogProps> = (props, uploadRef) => {
  const [visible, setVisible] = useState(false);
  const formRef = useRef<ProFormInstance>();
  const [confirmLoading, setConfirmLoading] = useState(false);
  const [initialValues, setInitialValues] = useState({});
  const [controlMode, setControlMode] = useState(0);

  const [formLayoutType, setFormLayoutType] = useState<FormLayout>(LAYOUT_TYPE_HORIZONTAL);
  const [grid, setGrid] = useState(true);
  const access = useAccess();
  const [tab, setTab] = useState('');
  const [tabPosition, setTabPosition] = useState<ProCardTabsProps['tabPosition']>('top');
  const location = useLocation();
  const [vmaxEposLu, setVmaxEposLu] = useState(0);

  useEffect(() => {
    setVisible(props.visible);
    setInitialValues(props.initialValues || {});
  }, [props.visible, props.initialValues]);

  useEffect(() => {
    if (location.query.id !== undefined && location.query.id !== null) {
      formRef.current?.setFieldsValue(props.values);
    }
  }, [props.values]);

  useImperativeHandle(uploadRef, () => {
    // return返回的值就可以被父组件获取到,没返回的值就获取不到
    return {
      // 获取其中的值
      getFieldsValue: () => {
        return formRef.current?.getFieldsValue();
      },
      // 设置值
      setFieldsValue: (value: object) => {
        // console.log(formRef.current);
        // debugger;
        return formRef.current?.setFieldsValue({ shaftName: '测试', hardwareIdentifier: 'hhh' });
      },
      // 重置其中的值
      resetFields: () => {
        return formRef.current?.resetFields();
      },
    };
  });

  return (
    <div>
      <ModalForm
        title={props.title}
        visible={visible}
        formRef={formRef}
        initialValues={initialValues}
        onFinish={async () => {
          setConfirmLoading(true);
          await props.onOk();
          setConfirmLoading(false);
          return true;
        }}
        onValuesChange={(changeValues) => { 
          if (changeValues.ratedPower != null && changeValues.ratedPower != undefined
            && formRef.current?.getFieldValue('ratedSpeed') != null) {
            let tempRp = formRef.current?.getFieldValue('ratedPower');
            let tempRs = formRef.current?.getFieldValue('ratedSpeed');
            formRef.current?.setFieldsValue({
              ratedTorque: 9550 * tempRp / tempRs,
            });
          }
          if (changeValues.ratedSpeed != null && changeValues.ratedSpeed != undefined
            && formRef.current?.getFieldValue('ratedPower') != null) {
            let tempRp = formRef.current?.getFieldValue('ratedPower');
            let tempRs = formRef.current?.getFieldValue('ratedSpeed');
            formRef.current?.setFieldsValue({
              ratedTorque: (9550 * tempRp / tempRs).toFixed(3),
            });
          }
        }}
        modalProps={{
          destroyOnClose: true,
          bodyStyle: { height: 500, overflowY: 'scroll' },
          onCancel: () => props.onCancel(),
        }}
        layout={formLayoutType}
        grid={grid}
        rowProps={{
          gutter: [16, formLayoutType === 'inline' ? 16 : 0],
        }}
      >
        <ProFormGroup
          key="lib1"
          title="基础信息配置"
          titleStyle={{ backgroundColor: '#f0f0f0' }}
          collapsible
        >
          <ProFormText hidden={true} name="id" label="轴信息Id" />
          <ProFormText
            width="md"
            name="motorManufacturer"
            label="电机厂家"
            colProps={{ md: 12, xl: 8 }}
            rules={[{ required: true, message: '请输入电机厂家' }]}
          />
          <ProFormText
            width="md"
            name="motorOrderNum"
            label="电机订货号"
            colProps={{ md: 12, xl: 8 }}
            rules={[{ required: true, message: '请输入电机订货号' }]}
          />
          <ProFormSelect
            width="md"
            name="voltageType"
            label="电压类型"
            options={[
              { label: '230/400V Δ/Y', value: 0 },
              { label: '400/690V Δ/Y', value: 1 },
            ]}
            colProps={{ md: 12, xl: 8 }}
            rules={[{ required: true, message: '此项是必填项目' }]}
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="motorId"
            name="motorId"
            label="电机代码选择 P301"
            initialValue={0}
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="ratedVoltage"
            name="ratedVoltage"
            label="额定电压 P304 [V]"
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="ratedCurrent"
            name="ratedCurrent"
            label="额定电流 P305 [A]"
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="ratedPower"
            name="ratedPower"
            label="额定功率 P307 [kW]"
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="ratedPowerCoeff"
            name="ratedPowerCoeff"
            label="额定功率因数 P308"
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="ratedFrequency"
            name="ratedFrequency"
            label="额定频率 P310 [Hz]"
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="ratedSpeed"
            name="ratedSpeed"
            label="额定速度 P311 [rpm]"
          />
          <ProFormDigit
            width="md"
            colProps={{ md: 12, xl: 8 }}
            key="ratedTorque"
            name="ratedTorque"
            label="额定转矩 P312 [Nm]"
          />
          <ProFormSelect
            width="md"
            name="connectionType"
            label="连接类型 P133.0"
            options={[
              { label: 'Y', value: 0 },
              { label: 'Δ', value: 1 },
            ]}
            colProps={{ md: 16, xl: 12 }}
          />
          <ProFormSelect
            width="md"
            name="is87Hz"
            label="87Hz运行选择 P133.1"
            options={[
              { label: '50Hz', value: 0 },
              { label: '87Hz', value: 1 },
            ]}
            colProps={{ md: 16, xl: 12 }}
          />
        </ProFormGroup>

        <ProFormGroup
          key="lib2"
          title="MotorData"
          titleStyle={{ backgroundColor: '#f0f0f0' }}
          collapsible
        >
          <ProFormText
            width="md"
            name="motorData1"
            label="motorData[1] P341 [kg·m²]"
            colProps={{ md: 16, xl: 12 }}
          />
          <ProFormText
            width="md"
            name="motorData2"
            label="motorData[2] P344 [kg]"
            colProps={{ md: 16, xl: 12 }}
          />
          <ProFormText
            width="md"
            name="motorData3"
            label="motorData[3] P320"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData4"
            label="motorData[4] P350"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData5"
            label="motorData[5] P354"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData6"
            label="motorData[6] P356"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData7"
            label="motorData[7] P358"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData8"
            label="motorData[8] P360"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData9"
            label="motorData[9] P1825"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData10"
            label="motorData[10] P1828"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData11"
            label="motorData[11] P1829"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData12"
            label="motorData[12] P1830"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData13"
            label="motorData[13]"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData14"
            label="motorData[14]"
            colProps={{ md: 12, xl: 8 }}
          />
          <ProFormText
            width="md"
            name="motorData15"
            label="motorData[15]"
            colProps={{ md: 12, xl: 8 }}
          />
        </ProFormGroup>
      </ModalForm>
    </div>
  );
};
const G120Common = forwardRef<unknown, FormDialogProps>(G120modal);

export default G120Common;
